<!DOCTYPE html>
<html>
<head>
    <title>002.数据库操作</title>

    <meta charset="UTF-8"/>

    <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>

    <meta name="HandheldFriendly" content="true"/>

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>

    <meta name="apple-mobile-web-app-capable" content="yes"/>

    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>

    <link href="./asset/farbox/basic.css" type="text/css" rel="stylesheet"/>
    <link href="./asset/farbox/main.css" type="text/css" rel="stylesheet"/>

    <style type="text/css">
        
            span.md_line{margin-bottom:0.5em; display:block; line-height:1.89}
            .md_line br{ display: none;}
            
    </style>

    <script>
        var flowchat_options = {
            'x': 0, 'y': 0, 'line-width': 1, 'line-length': 50, 'text-margin': 10, 'font-size': 13,
            'font-color': '#3c3c3c', 'line-color': '#666666', 'element-color': '#666666', 'fill': 'transparent',
            'yes-text': 'yes', 'no-text': 'no', 'arrow-end': 'block', 'class': 'flowchart', 'scale': 1,
            'symbols': { 'start': {}, 'end': {}, 'condition': {}, 'inputoutput': {}, 'operation': {}, 'subroutine': {}}
        }
    </script>
    <script type="text/javascript" src="./asset/other/raphael-min.js"></script>
    <script type="text/javascript" src="./asset/other/flowchart.js"></script>
    <script type="text/javascript" src="./asset/other/echarts.min.js"></script>


</head>

<body>
    
        <a href="./index.html" id="go_to_home"> &lt; home </a>
    
    <div id="sidebar">
        <ul id="sidebar_body">
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./001.软件下载安装配置.html" class="">第一章 软件下载安装配置</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./002.数据库操作.html" class="selected">002.数据库操作</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./003.数据表操作.html" class="">003.数据表操作</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./004.PHP与sql server连接.html" class="">004.PHP与sql server连接</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./005.数据的增删改.html" class="">005.数据的增删改</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./006.数据操作-简单查询.html" class="">006.数据操作-简单查询</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./007.高级查询.html" class="">07.高级查询-排序 多表联合 聚合与分组 限制条数</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./008.多表连接查询.html" class="">08.多表连接查询</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./009.子查询 ANY ALL [NOT] IN [NOT]EXISTS.html" class="">09.子查询 ANY ALL [NOT] IN [NOT]EXISTS</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./010.事务-索引-视图.html" class="">010.事务-索引-视图</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./011.T-SQL.html" class="">011.T-SQL学习</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./012.存储过程.html" class="">012.存储过程</a>
                </li>
            

            
        
            
            
            
            

            
                <li class="level_1 file">
                    <a href="./013.触发器.html" class="">10.触发器</a>
                </li>
            

            
        
        </ul>
    </div>

    <div id="main">
        <div class="content_body">
            <h1 class="title"> 002.数据库操作 </h1>
            <div class="doc_post"> <p class="md_block">

<div class="toc"><ul>
<li>
<a href="#toc_0">002.数据库操作</a>
<ul>
<li>
<a href="#toc_1">新建数据库</a>
</li>
<li>
<a href="#toc_2">删除数据库</a>
</li>
<li>
<a href="#toc_3">数据库重命名</a>
</li>
<li>
<a href="#toc_4">常用数据库操作</a>
</li>
</ul>
</li>
</ul>
</div>
</p>


<h2 id="toc_1">新建数据库</h2>

<p class="md_block">
    <span class="md_line">T-SQL语句是不区分大小写的.但有个开发规范:所有关键字大写,所有的自定义名称小写.</span>
</p>

<div class="codehilite code_lang_sql  highlight"><pre><span></span><span class="c1">-- 最简单的创建数据库语句:语法  CREATE DATABASE 数据库名;</span>
<span class="c1">-- CREATE DATABASE &quot;test&quot;;</span>

<span class="c1">-- 标准的创建数据库语句,指定参数.注意:size与 filegrowth的值不要用引号.</span>
<span class="k">CREATE</span> <span class="k">DATABASE</span> <span class="n">test</span> <span class="c1">-- 数据库一般加[]或&quot;&quot;包起来</span>
<span class="k">ON</span> <span class="k">PRIMARY</span> <span class="c1">-- 指定文件组,默认就是PRIMARY(可以不写)</span>
<span class="p">(</span>
    <span class="n">NAME</span> <span class="o">=</span> <span class="ss">&quot;test&quot;</span> <span class="c1">-- 逻辑名称</span>
    <span class="p">,</span><span class="n">FILENAME</span> <span class="o">=</span> <span class="ss">&quot;D:\dbs\test2.mdf&quot;</span> <span class="c1">-- 路径</span>
    <span class="p">,</span><span class="k">SIZE</span> <span class="o">=</span> <span class="mi">5</span><span class="n">MB</span>
    <span class="p">,</span><span class="n">FILEGROWTH</span> <span class="o">=</span> <span class="mi">2</span><span class="n">MB</span> <span class="c1">-- 数据满后自动增长的  增量.可以是百分比或一个值(如果是值要带单位).</span>
<span class="p">)</span>
<span class="n">LOG</span> <span class="k">ON</span>
<span class="p">(</span>
    <span class="n">NAME</span> <span class="o">=</span> <span class="ss">&quot;test_log&quot;</span>
    <span class="p">,</span><span class="n">FILENAME</span> <span class="o">=</span> <span class="ss">&quot;D:\dbs\test2_log.ldf&quot;</span>
    <span class="p">,</span><span class="k">SIZE</span> <span class="o">=</span> <span class="mi">1</span><span class="n">MB</span>
    <span class="p">,</span><span class="n">MAXSIZE</span> <span class="o">=</span> <span class="mi">100</span><span class="n">MB</span> <span class="c1">-- 指定文件的最大大小</span>
    <span class="p">,</span><span class="n">FILEGROWTH</span> <span class="o">=</span> <span class="mi">10</span><span class="o">%</span>
<span class="p">);</span>
</pre></div>

<!--block_code_end-->
<h2 id="toc_2">删除数据库</h2>
<div class="codehilite code_lang_sql  highlight"><pre><span></span><span class="c1">-- 删除数据库</span>
<span class="k">DROP</span> <span class="k">DATABASE</span> <span class="n">test</span><span class="p">;</span>
</pre></div>

<!--block_code_end-->
<h2 id="toc_3">数据库重命名</h2>
<div class="codehilite code_lang_sql  highlight"><pre><span></span><span class="c1">-- 数据库重命名(利用SQL内存的存储过程来完成)</span>
<span class="k">exec</span> <span class="n">sp_renamedb</span> <span class="ss">&quot;test01&quot;</span><span class="p">,</span><span class="ss">&quot;test02&quot;</span><span class="p">;</span>
</pre></div>

<!--block_code_end-->
<h2 id="toc_4">常用数据库操作</h2>
<div class="codehilite code_lang_sql  highlight"><pre><span></span><span class="c1">-- 得到目前所有的数据库名....</span>
<span class="k">SELECT</span> <span class="n">name</span> <span class="k">FROM</span> <span class="n">master</span><span class="p">.</span><span class="n">sys</span><span class="p">.</span><span class="n">sysdatabases</span> <span class="p">;</span>

<span class="c1">-- 如果数据库不存才创建.</span>
<span class="k">IF</span><span class="p">(</span><span class="k">NOT</span> <span class="k">EXISTS</span><span class="p">(</span><span class="k">SELECT</span> <span class="n">name</span> <span class="k">FROM</span> <span class="n">master</span><span class="p">.</span><span class="n">sys</span><span class="p">.</span><span class="n">sysdatabases</span> <span class="k">WHERE</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;test&#39;</span><span class="p">))</span>
     <span class="k">CREATE</span> <span class="k">DATABASE</span> <span class="n">test</span><span class="p">;</span>
     
<span class="c1">-- 如果数据库存在,才删除它.(避免报错)</span>
<span class="k">IF</span><span class="p">(</span><span class="k">EXISTS</span><span class="p">(</span><span class="k">SELECT</span> <span class="n">name</span> <span class="k">FROM</span> <span class="n">master</span><span class="p">.</span><span class="n">sys</span><span class="p">.</span><span class="n">sysdatabases</span> <span class="k">WHERE</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;test&#39;</span><span class="p">))</span>
    <span class="k">DROP</span> <span class="k">DATABASE</span> <span class="n">test</span><span class="p">;</span>
</pre></div>

<!--block_code_end--> </div>
        </div>

    </div>





<script>
    // auto focus for menu
    window.onload = function(){
        var sidebar = document.getElementById('sidebar');
        var current = document.getElementsByClassName('selected')[0];
        if (current && sidebar){
            sidebar.scrollTop = current.offsetTop - sidebar.clientHeight/2 + 100
        }
    };
</script>




</body>

</html>